using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    public class CityRealAiProgressBarHUD : CityRealAiHUDBase
    {
        // 进度增减
        [SerializeField] 
        protected bool _Reverse = true;
        // 进度条
        public SpriteRenderer _ProgressBar;
        
        // 消失时长
        protected float _Duration;
        // 计时器
        public float Timer => _Timer;
        protected float _Timer;

        protected MaterialPropertyBlock _MPB;

        // Update is called once per frame
        protected virtual void Update()
        {
            if (_Duration <= 0)
            {
                return;
            }
            
            _Timer += Time.deltaTime;

            float progress = _Timer / _Duration;
            if (progress > 1)
            {
                progress = 1;
            }

            SetValue(progress);
        }

        public virtual void SetProgressBar(float duration)
        {
            _Timer = 0;
            _Duration = duration;

            SetValue(0);
        }
        
        void SetValue(float value)
        {
            if (!_Reverse)
            {
                value = 1 - value;
            }
            
            if (_MPB == null)
            {
                _MPB = new MaterialPropertyBlock();
                _ProgressBar.GetPropertyBlock(_MPB);
            }
            
            _MPB.SetFloat("_Amount", value);
            _ProgressBar.SetPropertyBlock(_MPB);
        }
    }
}
